/**
 * 
 */
package uk.ac.lkl.common.util;

//import java.util.Properties;
import java.lang.IllegalArgumentException;
import java.util.HashMap;
import java.util.HashSet;

/**
 * turn the cml line args into a property table.
 * use bash style, i.e. -option1 value1 -option2 value2
 * if no -, concat args.
 * 
 * @author yish
 *
 */
public class Args extends HashMap<String,String> {
	private static final long serialVersionUID = 1L;
	
	/**
	 * @param args the input from the cmd line
	 * @param params the expected param names
	 */
	public Args(String[] args, String[] params) throws IllegalArgumentException {
		super(10);
		if (args == null || args.length == 0 || 
				params == null || params.length == 0) return;
		//String key = null;
		HashSet<String> paramSet = new HashSet<String>();
		for (int i=0; i <params.length; i++) {
			paramSet.add(params[i]);
		}
				
		String key = null;
		String value = null;
		for (int i=0; i < args.length; i++) {
			if (args[i].startsWith("-")) {
				key = args[i].substring(1);
				if (!paramSet.contains(key)) throw new IllegalArgumentException(key);
				if (i == (args.length -1) || args[i+1].startsWith("-")) {
					value = "";
				} else {
					value = args[++i];
				}
				put(key,value);
			}
		}
	}
}
